#include "AppFramework.h"

using namespace Ogre;
template<> AppFramework* Ogre::Singleton<AppFramework>::msSingleton = 0;

AppFramework::AppFramework(const char* szPath)
:	m_pRoot(NULL)
,	m_pRenderWnd(NULL)
,   m_pViewport(NULL)
,   m_pLog(NULL)
,   m_pTimer(NULL)
,   m_pInputMgr(NULL)
,	m_pOverlaySystem(NULL)
,   m_pTrayMgr(NULL)
,	m_bInit(true)
,	m_szResourcePath(szPath)
{
}

AppFramework::~AppFramework()
{
	AppFramework::getSingletonPtr()->m_pLog->logMessage("Shutdown OGRE...");
    if (m_pInputMgr)
		OIS::InputManager::destroyInputSystem(m_pInputMgr);

	OGRE_SAFE_DELETE(m_pTrayMgr);
	OGRE_SAFE_DELETE(m_pOverlaySystem);
	OGRE_SAFE_DELETE(m_pRoot);
	

#ifdef OGRE_STATIC_LIB
	mStaticPluginLoader.unload();
#endif		    
}

bool AppFramework::InitFmk(Ogre::String wndTitle, OIS::KeyListener *pKeyListener, OIS::MouseListener *pMouseListener)
{
	Ogre::LogManager* logMgr = OGRE_NEW Ogre::LogManager();

    m_pLog = Ogre::LogManager::getSingleton().createLog("Tetris3DApp.log", true, true, false);
    m_pLog->setDebugOutputEnabled(true);

	m_pRoot = OGRE_NEW Ogre::Root("", m_szResourcePath + "ogre.cfg");

#ifdef OGRE_STATIC_LIB
	mStaticPluginLoader.load();
#endif 

 	//if(!m_pRoot->showConfigDialog())
	if (!m_pRoot->restoreConfig())
		return false;

	m_pOverlaySystem = OGRE_NEW Ogre::OverlaySystem();
    m_pRenderWnd = m_pRoot->initialise(true, wndTitle);

    m_pViewport = m_pRenderWnd->addViewport(0);
    m_pViewport->setBackgroundColour(ColourValue(0.5f, 0.5f, 0.5f, 1.0f));

    m_pViewport->setCamera(0);

    size_t hWnd = 0;
    OIS::ParamList paramList;
    m_pRenderWnd->getCustomAttribute("WINDOW", &hWnd);

    paramList.insert(OIS::ParamList::value_type("WINDOW", Ogre::StringConverter::toString(hWnd)));

    m_pInputMgr = OIS::InputManager::createInputSystem(paramList);

	m_inputContext.mKeyboard = static_cast<OIS::Keyboard*>(m_pInputMgr->createInputObject(OIS::OISKeyboard, true));
	m_inputContext.mMouse = static_cast<OIS::Mouse*>(m_pInputMgr->createInputObject(OIS::OISMouse, true));

	m_inputContext.mMouse->getMouseState().height = m_pRenderWnd->getHeight();
	m_inputContext.mMouse->getMouseState().width = m_pRenderWnd->getWidth();

	if (pKeyListener == 0)
		m_inputContext.mKeyboard->setEventCallback(this);
	else
		m_inputContext.mKeyboard->setEventCallback(pKeyListener);

    if(pMouseListener == 0)
		m_inputContext.mMouse->setEventCallback(this);
    else
		m_inputContext.mMouse->setEventCallback(pMouseListener);

    Ogre::String secName, typeName, archName;
    Ogre::ConfigFile cf;
	cf.load(m_szResourcePath + "resources.cfg");

    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
    while (seci.hasMoreElements())
    {
        secName = seci.peekNextKey();
        Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
        Ogre::ConfigFile::SettingsMultiMap::iterator i;
        for (i = settings->begin(); i != settings->end(); ++i)
        {
            typeName = i->first;
            archName = i->second;
			Ogre::ResourceGroupManager::getSingleton().addResourceLocation(m_szResourcePath + archName, typeName, secName);
        }
    }
    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

	m_pTrayMgr = new OgreBites::SdkTrayManager("AOFTrayMgr", m_pRenderWnd, m_inputContext, 0);
	m_pTimer = OGRE_NEW Ogre::Timer();
    m_pTimer->reset();

    m_pRenderWnd->setActive(true);

    return true;
}

void AppFramework::UpdateFmk(double fDeltaTime)
{
	if (m_bInit)
	{
		m_pTrayMgr->hideLogo();
		m_bInit = false;
	}
}

bool AppFramework::keyPressed(const OIS::KeyEvent &keyEventRef)
{
	if (m_inputContext.mKeyboard->isKeyDown(OIS::KC_SYSRQ))
    {
        m_pRenderWnd->writeContentsToTimestampedFile("AOF_Screenshot_", ".jpg");
        return true;
    }

	if (m_inputContext.mKeyboard->isKeyDown(OIS::KC_O))
    {
        if(m_pTrayMgr->isLogoVisible())
        {
            m_pTrayMgr->hideFrameStats();
            m_pTrayMgr->hideLogo();
        }
        else
        {
            //m_pTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
            //m_pTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
        }
    }

    return true;
}

bool AppFramework::keyReleased(const OIS::KeyEvent &keyEventRef)
{
    return true;
}

bool AppFramework::mouseMoved(const OIS::MouseEvent &evt)
{
    return true;
}

bool AppFramework::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
    return true;
}

bool AppFramework::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
    return true;
}


